import java.awt.event.KeyEvent;

/**
 * KeyDecoder decodes KeyEvents into KeyStrokes Author: Kyle Van Wie Version
 * v1.3 v1 Initial Version v1.2 Decoupled Version v1.3 Switched Data Structure
 */
public class KeyDecoder {

	DoublyLinkedList<String> KeyStrokes;

	public KeyDecoder() {
		KeyStrokes = new DoublyLinkedList<String>();
	}

	public DoublyLinkedList<String> getCommandQueue() {
		return KeyStrokes;
	}

	public String getCommand() {
		if (KeyStrokes.isEmpty()) {
			return null;
		} else {
			return KeyStrokes.removeFirst();
		}
	}

	void decode(KeyEvent e) {
		int KeyCode;
		int KeyMod;
		if (e == null) {
			KeyCode = 0;
			KeyMod = 0;
		} else {
			KeyCode = e.getKeyCode();
			KeyMod = e.getModifiers();
		}
		// KEY-PAD 1
		if (KeyCode == 97 && KeyMod == 0) {
			KeyStrokes.addBack("SW");
		}
		// KEY-PAD 2
		if (KeyCode == 98 && KeyMod == 0) {
			KeyStrokes.addBack("S");
		}
		// KEY-PAD 3
		if (KeyCode == 99 && KeyMod == 0) {
			KeyStrokes.addBack("SE");
		}
		// KEY-PAD 4
		if (KeyCode == 100 && KeyMod == 0) {
			KeyStrokes.addBack("W");
		}
		// KEY-PAD 5
		if (KeyCode == 101 && KeyMod == 0) {
			KeyStrokes.addBack("FOCUS");
		}
		// KEY-PAD 6
		if (KeyCode == 102 && KeyMod == 0) {
			KeyStrokes.addBack("E");
		}
		// KEY-PAD 7
		if (KeyCode == 103 && KeyMod == 0) {
			KeyStrokes.addBack("NW");
		}
		// KEY-PAD 8
		if (KeyCode == 104 && KeyMod == 0) {
			KeyStrokes.addBack("N");
		}
		// KEY-PAD 9
		if (KeyCode == 105 && KeyMod == 0) {
			KeyStrokes.addBack("NE");
		}
		// ENTER
		if (KeyCode == 10 && KeyMod == 0) {
			KeyStrokes.addBack("ENTER");
		}
		// CTRL + ARROW ->
		if (KeyCode == 39 && KeyMod == 2) {
			KeyStrokes.addBack("CTRL + RIGHT");
		}
		// CTRL + ARROW <-
		if (KeyCode == 37 && KeyMod == 2) {
			KeyStrokes.addBack("CTRL + LEFT");
		}
		// CTRL + ARROW v
		if (KeyCode == 40 && KeyMod == 2) {
			KeyStrokes.addBack("CTRL + DOWN");
		}
		// CTRL + ARROW ^
		if (KeyCode == 38 && KeyMod == 2) {
			KeyStrokes.addBack("CTRL + UP");
		}
		// ARROW ->
		if (KeyCode == 39 && KeyMod == 0) {
			KeyStrokes.addBack("RIGHT");
		}
		// ARROW <-
		if (KeyCode == 37 && KeyMod == 0) {
			KeyStrokes.addBack("LEFT");
		}
		// ARROW v
		if (KeyCode == 40 && KeyMod == 0) {
			KeyStrokes.addBack("DOWN");
		}
		// ARROW ^
		if (KeyCode == 38 && KeyMod == 0) {
			KeyStrokes.addBack("UP");
		}
		// Number 1
		if (KeyCode == 49 && KeyMod == 0) {
			KeyStrokes.addBack("1");
		}
		// Number 2
		if (KeyCode == 50 && KeyMod == 0) {
			KeyStrokes.addBack("2");
		}
		// Number 3
		if (KeyCode == 51 && KeyMod == 0) {
			KeyStrokes.addBack("3");
		}
		// Number 4
		if (KeyCode == 52 && KeyMod == 0) {
			KeyStrokes.addBack("4");
		}
		// Number 5
		if (KeyCode == 53 && KeyMod == 0) {
			KeyStrokes.addBack("5");
		}
		// Number 6
		if (KeyCode == 54 && KeyMod == 0) {
			KeyStrokes.addBack("6");
		}
		// Number 7
		if (KeyCode == 55 && KeyMod == 0) {
			KeyStrokes.addBack("7");
		}
		// Number 8
		if (KeyCode == 56 && KeyMod == 0) {
			KeyStrokes.addBack("8");
		}
		// Number 9
		if (KeyCode == 57 && KeyMod == 0) {
			KeyStrokes.addBack("9");
		}
		// Number 0
		if (KeyCode == 48 && KeyMod == 0) {
			KeyStrokes.addBack("0");
		}
		// Letter S
		if (KeyCode == 83 && KeyMod == 8) {
			KeyStrokes.addBack("Alt + S");
		}
	}

}
